//
// Created by 谢哲 on 2021/5/17.
//
#include <iostream>
#include <vector>
using namespace std;

int dfs(int pos, vector<pair<int, int>> &tree) {
    int ans = 1;
    if (tree[pos].first) ans = dfs(tree[pos].first, tree)+1;
    if (tree[pos].second) ans = max(ans, dfs(tree[pos].second, tree)+1);

    return ans;
}

int main() {
    int n;
    cin >> n;
    vector<pair<int, int>> tree(n+1);
    for (int i=1; i<=n; ++i) {
        cin >> tree[i].first >> tree[i].second;
    }

    cout << dfs(1, tree);

    return 0;
}